package com.zbj;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * CustomApplicationListener
 *
 * @author weigang
 * @create 2019-07-30
 **/
// 添加监听器方式二
//@Component
public class CustomApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 获取 ApplicationContext 方式二
        // 这时直接引用 @Autowired DefaultListableBeanFactory defaultListableBeanFactory 为空 由Listener执行时间可知
        ApplicationContext applicationContext = event.getApplicationContext();
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
        Order order = new Order("333333333333", "444444444", "5555555");
        defaultListableBeanFactory.registerSingleton("order2", order);
    }
}